﻿using System;
using System.ComponentModel;
using System.Globalization;
using System.IO;
using System.Threading;
using System.Windows.Media;
using Lottery.Client.Services.Remote;

namespace Lottery.Client.SL
{
    public class UserInfo : INotifyPropertyChanged
    {
        private PropertyChangedEventHandler _onPropertyChanged;
        public int ID;
        private string _nickName;
        private double _balance;
        private double _freeze;
        public double MaxBackPct;
        private double _backPct;
        public short UserType;
        public int CommenderID;
        private static SolidColorBrush _brush1;
        private static SolidColorBrush _brush2;
        private static SolidColorBrush _brush3;
        public bool _status;
        private short _amountLave;
        private int _accountLimit;
        private string _agentPermissions;
        private string _sessionID;
        public event PropertyChangedEventHandler PropertyChanged
        {
            add
            {
                PropertyChangedEventHandler propertyChangedEventHandler = this._onPropertyChanged;
                PropertyChangedEventHandler propertyChangedEventHandler2;
                do
                {
                    propertyChangedEventHandler2 = propertyChangedEventHandler;
                    PropertyChangedEventHandler propertyChangedEventHandler3 = (PropertyChangedEventHandler)Delegate.Combine(propertyChangedEventHandler2, value);
                    propertyChangedEventHandler = Interlocked.CompareExchange<PropertyChangedEventHandler>(ref this._onPropertyChanged, propertyChangedEventHandler3, propertyChangedEventHandler2);
                }
                while (propertyChangedEventHandler != propertyChangedEventHandler2);
            }
            remove
            {
                PropertyChangedEventHandler propertyChangedEventHandler = this._onPropertyChanged;
                PropertyChangedEventHandler propertyChangedEventHandler2;
                do
                {
                    propertyChangedEventHandler2 = propertyChangedEventHandler;
                    PropertyChangedEventHandler propertyChangedEventHandler3 = (PropertyChangedEventHandler)Delegate.Remove(propertyChangedEventHandler2, value);
                    propertyChangedEventHandler = Interlocked.CompareExchange<PropertyChangedEventHandler>(ref this._onPropertyChanged, propertyChangedEventHandler3, propertyChangedEventHandler2);
                }
                while (propertyChangedEventHandler != propertyChangedEventHandler2);
            }
        }
        public string Name
        {
            get;
            set;
        }
        public short OnlineStatus
        {
            get
            {
                if (!string.IsNullOrEmpty(this.SessionID))
                {
                    return 1;
                }
                return 0;
            }
        }
        public string NickName
        {
            get
            {
                return this._nickName;
            }
            set
            {
                if (this._nickName == value) return;
                this._nickName = value;
                if (this._onPropertyChanged != null)
                {
                    this._onPropertyChanged.Invoke(this, new PropertyChangedEventArgs("NickName"));
                }
            }
        }
        public double Balance
        {
            get
            {
                return this._balance;
            }
            set
            {
                if (this._balance == value) return;
                this._balance = value;
                if (this._onPropertyChanged != null)
                {
                    this._onPropertyChanged.Invoke(this, new PropertyChangedEventArgs("Balance"));
                }
            }
        }
        public double Freeze
        {
            get
            {
                return this._freeze;
            }
            set
            {
                if (this._freeze == value) return;
                this._freeze = value;
                if (this._onPropertyChanged != null)
                {
                    this._onPropertyChanged.Invoke(this, new PropertyChangedEventArgs("Freeze"));
                }
            }
        }
        public double BackPct
        {
            get
            {
                return this._backPct;
            }
            set
            {
                if (this._backPct != value)
                {
                    this._backPct = value;
                    if (this._onPropertyChanged != null)
                    {
                        this._onPropertyChanged.Invoke(this, new PropertyChangedEventArgs("BackPct"));
                    }
                }
            }
        }
        public string UserTypeName
        {
            get
            {
                switch (this.UserType)
                {
                    case 0:
                        return "UserType0";
                    case 1:
                        return "UserType1";
                    default:
                        return "";
                }
            }
        }

        private string _levels;
        public string Levels
        {
            get { return _levels; }
            set { _levels = value; }
        }

        public SolidColorBrush UserTypeBrush
        {
            get
            {
                switch (this.UserType)
                {
                    case 1:
                        return UserInfo._brush1;
                    case 2:
                        return UserInfo._brush2;
                    default:
                        return UserInfo._brush3;
                }
            }
        }
        public bool IsCanLogin
        {
            get
            {
                return this._status;
            }
            set
            {
                if (this._status != value)
                {
                    this._status = value;
                    if (this._onPropertyChanged != null)
                    {
                        //this._onPropertyChanged.Invoke(this, new PropertyChangedEventArgs(BcZJxKMIDcH0kvSFls.w4WekCpilj(21562)));
                        //this._onPropertyChanged.Invoke(this, new PropertyChangedEventArgs(BcZJxKMIDcH0kvSFls.w4WekCpilj(21586)));
                        //this._onPropertyChanged.Invoke(this, new PropertyChangedEventArgs(BcZJxKMIDcH0kvSFls.w4WekCpilj(21610)));
                    }
                }
            }
        }
        public string StatusName
        {
            get
            {
                if (this.IsCanLogin)
                {
                    return "StatusNameIsCanLogin";
                }
                return "StatusNameIsNotCanLogin";
            }
        }
        public SolidColorBrush StatusBrush
        {
            get
            {
                if (this.IsCanLogin)
                {
                    return UserInfo._brush1;
                }
                return UserInfo._brush2;
            }
        }
        public string LastLoginTime
        {
            get;
            set;
        }
        public short AmountLave
        {
            get
            {
                return this._amountLave;
            }
            set
            {
                if (this._amountLave != value)
                {
                    this._amountLave = value;
                    if (this._onPropertyChanged != null)
                    {
                        this._onPropertyChanged.Invoke(this, new PropertyChangedEventArgs("AmountLave"));
                    }
                }
            }
        }
        public int AccountLimit
        {
            get
            {
                return this._accountLimit;
            }
            set
            {
                if (this._accountLimit != value)
                {
                    this._accountLimit = value;
                    if (this._onPropertyChanged != null)
                    {
                        this._onPropertyChanged.Invoke(this, new PropertyChangedEventArgs("AccountLimit"));
                    }
                }
            }
        }
        public string AgentPermissions
        {
            get
            {
                return this._agentPermissions;
            }
            set
            {
                if (this._agentPermissions != value)
                {
                    this._agentPermissions = value;
                    if (this._onPropertyChanged != null)
                    {
                        this._onPropertyChanged.Invoke(this, new PropertyChangedEventArgs("AgentPermissions"));
                    }
                }
            }
        }
        public string SessionID
        {
            get
            {
                return this._sessionID;
            }
            set
            {
                if (this._sessionID != value)
                {
                    this._sessionID = value;
                    if (this._onPropertyChanged != null)
                    {
                        this._onPropertyChanged.Invoke(this, new PropertyChangedEventArgs("SessionID"));
                    }
                }
            }
        }
        public UserInfo()
		{
			this._nickName = "";
			this.CommenderID = -1;
			this._status = true;
			this._agentPermissions = "";
			this._sessionID = "";
		}
        static UserInfo()
        {
            UserInfo._brush1 = new SolidColorBrush(Color.FromArgb(255, 0, 255, 0));
            UserInfo._brush2 = new SolidColorBrush(Colors.Red);
            UserInfo._brush3 = new SolidColorBrush(Colors.Gray);
        }
        public static UserInfo GetUserInfo(byte[] bytes)
        {
            if (bytes == null || bytes.Length < 1)
            {
                return null;
            }
            MemoryStream memoryStream = new MemoryStream(bytes);
            BinaryReader binaryReader = new BinaryReader(memoryStream);
            UserInfo userInfo = UserInfo.GetUserInfo(binaryReader);
            binaryReader.Close();
            memoryStream.Close();
            return userInfo;
        }
        public static UserInfo GetUserInfo(UserInfoResult ur)
        {
            var ui = new UserInfo();
            ui.Name = DataCache.CurrentLoginInfo.UserName;
            ui.Freeze = Convert.ToDouble(ur.Lockmoney);
            ui.Balance = Convert.ToDouble(ur.Usermoney);
            ui.Levels = ur.Levels;
            return ui;
        }
        public static UserInfo GetUserInfo(BinaryReader br)
        {
            UserInfo result;
            try
            {
                UserInfo userInfo = new UserInfo();
                userInfo.ID = br.ReadInt32();
                userInfo.Name = br.ReadString();
                userInfo.NickName = br.ReadString();
                userInfo.Balance = br.ReadDouble();
                userInfo.Freeze = br.ReadDouble();
                userInfo.MaxBackPct = br.ReadDouble();
                userInfo.BackPct = br.ReadDouble();
                userInfo.UserType = br.ReadInt16();
                userInfo.IsCanLogin = br.ReadBoolean();
                userInfo.CommenderID = br.ReadInt32();
                try
                {
                    userInfo.LastLoginTime = Convert.ToDateTime(br.ReadString()).ToString(CultureInfo.InvariantCulture);
                }
                catch (FormatException)
                {
                    userInfo.LastLoginTime = "";
                }
                userInfo.AmountLave = br.ReadInt16();
                userInfo.AccountLimit = br.ReadInt32();
                userInfo.AgentPermissions = br.ReadString();
                userInfo.SessionID = br.ReadString();
                result = userInfo;
            }
            catch
            {
                result = null;
            }
            return result;
        }
    }
}
